package org.leviatan.dataharbour.gui.swingmodels;

import java.util.Iterator;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

import org.leviatan.dataharbour.core.model.DataEntity;

public class TableMdlDataUnits extends AbstractTableModel {

	private String[] columnNames = {"Id","Name", "Length(Bytes)", "Value"};
	
	private Object[][] tableData;
	
	private static final long serialVersionUID = -8962042943625322979L;
	
	public void setData(Map variableMap) {
		this.tableData = new Object[variableMap.size()][4];
		int col = 0;
		for (Iterator i = variableMap.values().iterator(); i.hasNext(); ) {
			DataEntity dataEnt = (DataEntity) i.next();
			
			String varId = dataEnt.getId();
			String varName = dataEnt.getDataName();
			String varValue = dataEnt.getDataValue();
			int length = varName.length() * 2; // 2 bytes = 16 bit / char
			
			tableData[col][0] = varId;
			tableData[col][1] = varName;
			tableData[col][2] = length;
			tableData[col][3] = varValue;
			
			col++;
		}
	}
	
	public String getColumnName(int i) {
		return this.columnNames[i];
	}
	
	public int getColumnCount() {
		return this.columnNames.length;
	}

	public int getRowCount() {
		if (tableData == null || tableData.length <= 0)
			return 0;
		return tableData.length;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return this.tableData[rowIndex][columnIndex];
	}
	
	

}
